<?php

namespace app\api\controller\web;


use app\admin\model\Org;
use app\admin\model\Role;
use app\admin\model\Shopcharge;
use app\admin\model\Shopchargeconfig;
use app\admin\model\Shopdaytrade;
use app\admin\model\Shopdevice;
use app\admin\model\Shoporder;
use app\admin\model\Shoproomset;
use app\admin\model\Shoptuan;
use app\admin\model\Shopwifi;
use app\admin\model\Tradeday;
use app\admin\model\Tuanorder;
use app\common\controller\Api;
use OSS\OssClient;
use PhpOffice\PhpSpreadsheet\IOFactory;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use think\Config;

/**
 *
 */
class Trade extends Api
{
    protected $noNeedLogin = [];
    protected $noNeedRight = ['*'];




    public function getList()
    {
        $user = $this->auth->getUser();
        $shop_id = $this->request->param('shop_id');
        $page = $this->request->param('page');
        $startDate = $this->request->param('startDate');
        $endDate = $this->request->param('endDate');


        $limit = 10;
        $offset = ($page - 1) * $limit;

        $orgModel = new Org();
        $org = $orgModel->where('mobile',$user['mobile'])->find();

        if(!$org){
            $roleModel = new Role();
            $role = $roleModel->where('mobile',$user['mobile'])->find();
            $org = $orgModel->where('id',$role['org_id'])->find();
        }

        $where = array();
        $where['org_id'] = $org['id'];

        if($shop_id != ""){
            $where['shop_id'] = $shop_id;
        }
        if($startDate !=""){
            $startTime = $startDate." 00:00:00";
            $endTime = $endDate." 23:59:59";
            $where['create_time'] = ['between',[strtotime($startTime),strtotime($endTime)]];
        }

        $shopTradeModel = new Shopdaytrade();
        $list = $shopTradeModel->where($where)->limit($offset,$limit)->order('create_time','desc')->select();
        $count = $shopTradeModel->where($where)->count('id');
        $amount = $shopTradeModel->where($where)->sum('book_amount');

        foreach ($list as $key=>$val){

            $list[$key]['create_time_text'] = date("Y-m-d",$val['create_time']);
        }

        $ret = array();
        $ret['total'] =$count;
        $ret['list'] =$list;
        $ret['amount'] = $amount;

        $this->success('请求成功', $ret);
    }


    public function countExportTrade(){

        $startDate = $this->request->param('startDate');
        $endDate = $this->request->param('endDate');
        $shop_id = $this->request->param('shop_id');

        $user = $this->auth->getUser();

        $orgModel = new Org();
        $org = $orgModel->where('mobile',$user['mobile'])->find();

        if(!$org){
            $roleModel = new Role();
            $role = $roleModel->where('mobile',$user['mobile'])->find();
            $org = $orgModel->where('id',$role['org_id'])->find();
        }

        $where = array();
        $where['Shopdaytrade.org_id'] = $org['id'];

        if($shop_id != ""){
            $where['shop_id'] = $shop_id;
        }
        if($startDate !=""){
            $startTime = $startDate." 00:00:00";
            $endTime = $endDate." 23:59:59";
            $where['create_time'] = ['between',[strtotime($startTime),strtotime($endTime)]];
        }

        $shopTradeModel = new Shopdaytrade();

        $list = $shopTradeModel->with(['shop'])->where($where)->order(['create_time' => 'desc', 'book_amount' => 'desc'])->select();
        $ret = array();
        foreach ($list as $key => $val) {
            $obj = array();
            $obj['date'] = date("Y-m-d", $val['create_time']);
            $obj['shopName'] = $val['shop']['name'];
            $obj['bookAmount'] = $val['book_amount'] ;
            $obj['bookPayAmount'] = $val['book_pay_amount'];
            $obj['chargeAmount'] = $val['charge_amount'] ;
            $obj['tuanAmount'] = $val['tuan_amount'];
            $obj['douAmount'] = $val['dou_amount'];
            $obj['mtAmount'] = $val['mt_amount'];
            $obj['refundAmount'] = $val['refund_amount'];
            $obj['profit'] = $val['profit'];
            $obj['balanceAmount'] = $val['balance_amount'];

            $ret[] = $obj;
        }

        $objExcel = new Spreadsheet();
        $objWriter = IOFactory::createWriter($objExcel, 'Xlsx');
        $objActSheet = $objExcel->getActiveSheet(0);
        $objActSheet->setTitle('日交易统计');//设置excel的标题

        $keys = ['日期', '店铺名字', '预定总金额', '在线支付金额', '充值金额', '团购券购买', '抖音核销', '美团核销','退款','平台抽成','余额抵扣'];
        array_unshift($ret, $keys);
        $objActSheet->fromArray($ret);
        header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
        $objWriter->save('php://output');

    }

    public function orderExportTrade(){

        $startDate = $this->request->param('startDate');
        $endDate = $this->request->param('endDate');
        $shop_id = $this->request->param('shop_id');

        $user = $this->auth->getUser();

        $orgModel = new Org();
        $org = $orgModel->where('mobile',$user['mobile'])->find();

        if(!$org){
            $roleModel = new Role();
            $role = $roleModel->where('mobile',$user['mobile'])->find();
            $org = $orgModel->where('id',$role['org_id'])->find();
        }

        $where = array();
        $where['org_id'] = $org['id'];

        if($shop_id != ""){
            $where['shop_id'] = $shop_id;
        }
        if($startDate !=""){
            $startTime = $startDate." 00:00:00";
            $endTime = $endDate." 23:59:59";
            $where['create_time'] = ['between',[strtotime($startTime),strtotime($endTime)]];
        }

        $orderModel = new Shoporder();
        $list = $orderModel->where($where)->where('status','not in',['paying','refund'])->order('create_time','desc')->select();
        $ret = array();
        foreach ($list as $key => $val) {
            $obj = array();
            $obj['date'] = date("Y-m-d", $val['create_time']);
            $obj['shopName'] = $val['shop_name'];
            $obj['roomName'] = $val['room_name'] ;
            $obj['orderNo'] = $val['order_no'];
            $obj['amount'] = $val['amount'] ;
            $obj['status'] = $orderModel->formatStatus($val['status']);
            $obj['bookStart'] = date("Y-m-d H:i",$val['book_start']);
            $obj['bookEnd'] = date("Y-m-d H:i",$val['book_end']);
            $obj['balance'] = $val['balance'];
            $obj['mtCode'] = $val['mt_code'];
            $obj['douCode'] = $val['dou_code'];
            $ret[] = $obj;
        }

        $objExcel = new Spreadsheet();
        $objWriter = IOFactory::createWriter($objExcel, 'Xlsx');
        $objActSheet = $objExcel->getActiveSheet(0);
        $objActSheet->setTitle('日交易统计');//设置excel的标题

        $keys = ['日期', '店铺名字', '包厢名字', '订单号', '总金额', '状态', '预定开始时间', '预定结束时间','余额抵扣','美团核销码','抖音核销码'];
        array_unshift($ret, $keys);
        $objActSheet->fromArray($ret);
        header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
        $objWriter->save('php://output');
    }


}
